<?php
/**
 * Created by PhpStorm.
 * Login: 曹旺
 * Date: 2018-03-08
 * Time: 17:47
 */

namespace app\index\controller;

use app\index\model\SmsMessage;
use app\index\model\UserAddress;
use think\Cache;
use think\Db;
use think\Loader;
use think\Session;
use think\Request;
use think\Validate;

class User extends Base
{
    /**
     * @return array
     * 登录
     * @author  曹旺
     * @data  11:24
     */
    public function login()
    {
        $result = '';
        if ($this->request->isPost()) {
            //$request=Request::instance();
            //$data=$request->except('token');
            $data['password'] = input('post.password');
            $phone = input('post.phone');
            /*if (isset($phone)) {
                $data['phone'] = $phone;
                $validate = Loader::validate('Login');
            }
            $user_card = input('post.user_card');
            if (isset($user_card)) {
                //会员卡以后在加目前还没启用
                $data['user_card'] = $user_card;
                $validate = Loader::validate('Usercard');
            }*/
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/login";
            $key = input('post.sign');
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if (!$res_pass) {
                $json_result = ['code' => 199, 'msg' => "签名未通过", 'content' => $result];
                return $json_result;
            }
            $validate = Loader::validate('Login');
            //验证手机和密码
            $data['phone'] = $phone;
            if (!$validate->check($data)) {
                unset($data['phone']);
                $data['user_card'] = $phone;
            }
            /*$json_result = ['code' => 200, 'msg' => "登录成功", 'content' => $data];
            return $json_result;*/
            $res = new \app\index\model\User();
            $data['password'] = md5(input('post.password'));
            $result = $res->user_login($data);
            //判断该账号用户是否唯一
            if (count($result) == 1) {
                //return json_encode(count($result['userinfo']));
                //Session::set('sign',md5(md5($data['email']).md5($result['userinfo']['id'])));
                //$session=Session::get('sign');
                //登录状态信息
                $results['userinfo'] = $result[0];
                if (isset($data['phone'])){
                    $results['token'] = md5($data['phone']);
                }else{
                    $results['token'] = md5($data['user_card']);
                }
                Cache::set($results['token'], $results['token'], 5184000);
                Db::table('nbsm_user')->where($data)->where('state=1')->update(['sign' => $results['token']]);
                //return json_encode($result);
                $json_result = ['code' => 200, 'msg' => "登录成功", 'content' => $results];
                return $json_result;
            } else {
                if ($result > 1) {
                    $json_result = ['code' => -1, 'msg' => "密码或账号验证错误", 'content' => $result];
                    return $json_result;
                } else {
                    $json_result = ['code' => 199, 'msg' => "密码或账号验证错误", 'content' => $result];
                    return $json_result;
                }
                /*$error = $validate->getError();
                $json_result = ['code' => 199, 'msg' => $error, 'content' => $result];
                return $json_result;*/
            }
            //return $result;
        } else {
            $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => $result];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 注册
     * @author  曹旺
     * @data  14:55
     */
    public function register()
    {
        //return 123456;
        if ($this->request->isPost()) {
            $request = request::instance();
            $data = $request->except('sign');
            $key = input('post.sign');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/register';
            /*$model = new \app\index\model\User();
            $result = $model->user_register($data);
            //return $result;
            $json_result = ['code' => 200, 'msg' => "注册成功", 'content' => $result];
            return $json_result;*/
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            //验证手机
            $phone_pass = $this->phone_pass($data['phone']);
            if ($phone_pass) {
                //验证手机和密码
                $validate = Loader::validate('Login');
                if ($validate->check($data)) {
                    $res_pass = $this->encryption($key, $value);
                    if ($res_pass) {
                        $model = new \app\index\model\User();
                        $result = $model->user_register($data);
                        //return $result;
                        /*$json_result = ['code' => 200, 'msg' => "注册成功", 'content' => $result];
                        return $json_result;*/
                        if ($result) {
                            if ($result == 1) {
                                $pass = md5($data['password']);
                                $myself = Db::table('nbsm_user')->field('id,nickname')->where(['phone' => $data['phone'], 'password' => $pass])->find();
                                $myselfinfo = md5($data['phone']);
                                Cache::set($myselfinfo, $myselfinfo, 5184000);
                                //$info=['id'=>1,'nickname'=>'aaaaaa'];
                                $my['userinfo'] = $myself;
                                $my['token'] = $myselfinfo;
                                //Db::table('nbsm_user')->where($data)->where('state=1')->update(['sign' => $myself['token']]);
                                $json_result = ['code' => 200, 'msg' => "注册成功", 'content' => $my];
                                return $json_result;
                            } elseif ($result == 3) {
                                $json_result = ['code' => -1, 'msg' => "验证码失效", 'content' => '注册失败'];
                                return $json_result;
                            } else {
                                $json_result = ['code' => -1, 'msg' => "手机号已被注册", 'content' => '注册失败'];
                                return $json_result;
                            }
                        } else {
                            $json_result = ['code' => -1, 'msg' => "注册失败", 'content' => '注册失败'];
                            return $json_result;
                        }
                    } else {
                        $json_result = ['code' => 199, 'msg' => "注册失败", 'content' => '非法入侵'];
                        return $json_result;
                    }
                } else {
                    $error = $validate->getError();
                    $json_result = ['code' => 199, 'msg' => "注册失败", 'content' => $error];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "注册失败", 'content' => '手机号错误'];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "注册失败", 'content' => '请求方式错误'];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 个人信息
     * @author  曹旺
     * @data  11:14
     */
    public function myself()
    {
        if ($this->request->isPost()) {
            $key = input('post.sign');
            $token = input('post.token');
            $user_id = input('post.id');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myself';
            //验证加密
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $json_result = ['code' => 199, 'msg' => "验证未通过", 'content' => ''];
                return $json_result;
            }
            //验证是否是登录状态
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "验证未通过", 'content' => ''];
                return $json_result;
            }
            $model = new \app\index\model\User();
            $result = $model->myselfs($user_id);
            if (!empty($result['userinfo'])) {
                $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => $result];
                return $json_result;
            } else {
                $json_result = ['code' => -1, 'msg' => "请求成功", 'content' => $result];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "请求方式错误", 'content' => ''];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 个人信息
     * @author  曹旺
     * @data  14:55
     */
    public function myselfinfo()
    {
        $result = '';
        if ($this->request->isPost()) {
            $data['id'] = input('post.id');
            $key = input('post.sign');
            $token = input('post.token');
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo";
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => $result];
                return $json_result;
            }
            $sign_res = $this->encryption($key, $value);
            if ($sign_res) {
                $model_info = new \app\index\model\User();
                $result = $model_info->user_myselfinfo($data);
                if (!empty($result)) {
                    $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => $result];
                    return $json_result;
                } else {
                    $json_result = ['code' => -1, 'msg' => "无此用户", 'content' => $result];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => $result];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "请求方式错误", 'content' => $result];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 个人信息的修改
     * @author  曹旺
     * @data  16:20
     */
    public function myselfinfo_select()
    {
        //$result='';
        if ($this->request->isPost()) {
            $request = Request::instance();
            $data = $request->except(['sign', 'token']);
            $key = input('post.sign');
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo_select";
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $pass_res = $this->encryption($key, $value);
            if ($pass_res) {
                $model = new \app\index\model\User();
                $result = $model->user_myselfinfo_select($data);
                if ($result) {
                    $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => '修改成功'];
                    return $json_result;
                } else {
                    $json_result = ['code' => -1, 'msg' => "请求成功", 'content' => '修改失败'];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => '管理员正在处理'];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "请求方式错误", 'content' => '联系管理员'];
            return $json_result;
        }
    }


    /**
     * @return mixed|string 用户id
     *  收货地址列表
     * @author  曹旺
     * @data  15:43
     */
    public function address_list()
    {
        $res = '';
        if ($this->request->isPost()) {
            $id = input('post.id');
            $key = input('post.sign');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/address_list';
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $res_pass = $this->encryption($key, $value);
            if (!$res_pass) {
                $res_json = ['code' => 199, 'msg' => '非法入侵', 'content' => $res];
                return $res_json;
            }
            $model = new UserAddress();
            $res = $model->address_lists($id);
            //return json_encode($res);
            if (!empty($res)) {
                $res_json = ['code' => 200, 'msg' => '请求成功', 'content' => $res];
                return $res_json;
            } else {
                $res_json = ['code' => -1, 'msg' => '请求成功', 'content' => $res];
                return $res_json;
            }
        } else {
            $res_json = ['code' => 199, 'msg' => '非法入侵', 'content' => $res];
            return $res_json;
        }
    }

    /**
     * @return mixed|string
     * 添加收货地址
     * @author  曹旺
     * @data  17:33
     */
    public function address_add()
    {
        if ($this->request->isPost()) {
            $request = Request::instance();
            $data = $request->except(['sign', 'token']);
            $key = input('post.sign');
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $phone_pass = $this->phone_pass($data['consignee_phone']);
            if ($phone_pass) {
                $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/address_add';
                $res_pass = $this->encryption($key, $value);
                if ($res_pass) {
                    $validate = Loader::validate('Address');
                    if ($validate->check($data)) {
                        $model = new UserAddress();
                        $result = $model->address_adds($data);
                        if ($result) {
                            $result_array['id'] = $result;
                            $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => $result_array];
                            return $json_result;
                        } else {
                            //添加失败
                            $json_result = ['code' => -1, 'msg' => "请求成功,添加失败", 'content' => ''];
                            return $json_result;
                        }
                    } else {
                        //数据格式错误
                        $error = $validate->getError();
                        $json_result = ['code' => 199, 'msg' => $error, 'content' => ''];
                        return $json_result;
                    }
                } else {
                    //管理员已处理
                    $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                    return $json_result;
                }
            } else {
                //请填写正确的手机号
                $json_result = ['code' => 199, 'msg' => "手机号格式错误", 'content' => ''];
                return $json_result;
            }
        } else {
            //管理员已处理
            $json_result = ['code' => 199, 'msg' => "请求方式错误", 'content' => ''];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 地址详情展示
     * @author  曹旺
     * @data  16:52
     */
    public function address_save_info()
    {
        if ($this->request->isPost()) {
            $key = input('post.sign');
            $id = input('post.id');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/address_save_info';
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            //return $token_pass;
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                $model = new UserAddress();
                $result = $model->address_save_infos($id);
                if (!empty($result)) {
                    $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => $result];
                    return $json_result;
                } else {
                    $json_result = ['code' => -1, 'msg' => "请求成功", 'content' => ''];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "请求失败", 'content' => '非法入侵'];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 200, 'msg' => "请求失败", 'content' => '请求方式错误'];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 修改收货地址
     * @author  曹旺
     * @data  17:33
     */
    public function address_save()
    {
        if ($this->request->isPost()) {
            $request = Request::instance();
            $data = $request->except(['sign', 'token']);
            $key = input('post.sign');
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $phone_pass = $this->phone_pass($data['consignee_phone']);
            if ($phone_pass) {
                $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/address_save';
                $res_pass = $this->encryption($key, $value);
                if ($res_pass) {
                    $validate = Loader::validate('Addresssave');
                    if ($validate->check($data)) {
                        $model = new UserAddress();
                        $result = $model->address_saves($data);
                        if ($result) {
                            $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => '修改成功'];
                            return $json_result;
                        } else {
                            $json_result = ['code' => -1, 'msg' => "请求成功", 'content' => '修改失败'];
                            return $json_result;
                        }
                    } else {
                        $error = $validate->getError();
                        $json_result = ['code' => 199, 'msg' => $error, 'content' => '数据格式错误'];
                        return $json_result;
                    }
                } else {
                    $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => '管理员已处理'];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "手机号格式错误", 'content' => '请填写正确的手机号'];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "请求方式错误", 'content' => '管理员已处理'];
            return $json_result;
        }
    }


    /**
     * @return mixed|string
     * 删除收货地址
     * @author  曹旺
     * @data  14:20
     */
    public function address_del()
    {
        if ($this->request->isPost()) {
            $key = input('post.sign');
            $id = input('post.id');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/address_del';
            //验证token
            $token = input('post.token');
            //$tt=Cache::get('token');
            /*if ($token == $tt){
                return 123;
            }*/
            //return 321;
            $token_pass = $this->encryption_token($token);
            //return $token_pass;
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                $model = new UserAddress();
                $result = $model->address_dels($id);
                if ($result) {
                    $json_result = ['code' => 200, 'msg' => "请求成功", 'content' => '删除成功'];
                    return $json_result;
                } else {
                    $json_result = ['code' => -1, 'msg' => "请求成功", 'content' => '删除失败'];
                    return $json_result;
                }
            } else {
                $json_result = ['code' => 199, 'msg' => "请求失败", 'content' => '非法入侵'];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 200, 'msg' => "请求失败", 'content' => '请求方式错误'];
            return $json_result;
        }
    }


    /**
     * @return mixed|string
     *  送货省份
     * @author  曹旺
     * @data  10:32
     */
    public function select_province()
    {
        if ($this->request->isPost()) {
            $key = input('post.sign');
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/select_province';
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                $model = new UserAddress();
                $result = $model->address();
                if (!empty($result)) {
                    $result_json = ['code' => 200, 'msg' => '请求成功', 'content' => $result];
                    return $result_json;
                } else {
                    $result_json = ['code' => -1, 'msg' => '请求成功', 'content' => $result];
                    return $result_json;
                }
            } else {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
                return $result_json;
            }
        } else {
            $result_json = ['code' => -1, 'msg' => '请求方式错误', 'content' => ''];
            return $result_json;
        }
    }

    /**
     * 发送验证码修改绑定
     * @author  曹旺
     * @data  17:33
     */
    public function myselfinfo_rand()
    {
        if ($this->request->isPost()) {
            $email['email'] = input('post.email');
            //用户id
            $phone = input('post.phone');
            $type = input('post.type');
            $key = input('post.sign');
            //验证token
            //$token = input('post.token');
            if ($type != '修改密码' && $type != "绑定" && $type != "忘记密码") {
                $json_result = ['code' => 199, 'msg' => "验证码类型有误", 'content' => ''];
                return $json_result;
            }
            /* $token_pass = $this->encryption_token($token);
             if (!$token_pass) {
                 $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                 return json_encode($json_result);
             }*/
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo_rand";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if (!$res_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
                return $result_json;
            }
            $model = new \app\index\model\User();
            if (isset($email) && $email['email'] != '') {
                //验证码
                $rand = $this->sendRand();
                //验证邮箱
                $validate = Loader::validate('Sendemail');
                if ($validate->check($email)) {
                    //用户id
                    /*$id = input('post.id');
                    //验证是否是用户对应的邮箱
                    $user_is = $model->is_user($id, $email);
                    if (!$user_is) {
                        $result_json = ['code' => 199, 'msg' => '用户不存在', 'content' => ''];
                        return json_encode($result_json);
                    }*/
                    $send_email = $this->email($email['email'], $rand, $type);
                    //return json_encode($send_email);
                    if ($send_email) {
                        //return 123;
                        //验证码有效期30分钟
                        Cache::set($email['email'] . $type . 'rand', $rand, 1800);
                        //$cache=Cache::get($email['email'] . $type.'rand');
                        //return json_encode($cache);
                        $result_json = ['code' => 200, 'msg' => '邮件发送成功', 'content' => '邮件发送成功'];
                        return $result_json;
                    } else {
                        $result_json = ['code' => -1, 'msg' => '邮件发送失败', 'content' => '邮件发送失败'];
                        return $result_json;
                    }
                } else {
                    //return 221;
                    $error = $validate->getError();
                    $result_json = ['code' => 199, 'msg' => '邮箱格式错误', 'content' => $error];
                    return $result_json;
                }
                //return 223;
                //发送邮件

            } elseif (isset($phone) && $phone != '') {
                $date = date('Y-m-d');
                $phone_rand_count = Cache::get($phone . $date . $type . 'count');
                if ($phone_rand_count >= 15) {
                    $result_json = ['code' => 199, 'msg' => '每天最多发送5条', 'content' => ''];
                    return $result_json;
                }
                $phone_pass = $this->phone_pass($phone);
                if (!$phone_pass) {
                    $result_json = ['code' => 199, 'msg' => '手机格式错误', 'content' => ''];
                    return $result_json;
                }
                //return 123;
                $send = new SmsMessage();
                $rand = $this->send_phoneRand();
                $arr = $send::sendSms($phone, $rand);
                //return json_encode($arr);
                $arr_array = json_decode(json_encode($arr), true);
                //return json_encode($arr_array);
                if ($arr_array['Code'] == 'OK') {
                    $phone_rand_count += 1;
                    Cache::set($phone . $date . $type . 'count', $phone_rand_count, strtotime('+1 day'));
                    Cache::set($phone . $type . "rand", $rand, 300);
                    $result_json = ['code' => 200, 'msg' => '发送成功', 'content' => '发送成功'];
                    return $result_json;
                } else {
                    $result_json = ['code' => 199, 'msg' => $arr_array['Code'], 'content' => ''];
                    return $result_json;
                }
            } else {
                $result_json = ['code' => 199, 'msg' => '账号信息不正确', 'content' => ''];
                return $result_json;
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '请求方式错误', 'content' => ''];
            return $result_json;
        }
    }


    /**
     * @return mixed|string
     * 注册忘记发送验证码
     * @author  曹旺
     * @data  09:39
     */
    public function register_rand()
    {
        if ($this->request->isPost()) {
            //用户id
            $phone = input('post.phone');
            $type = input('post.type');
            $key = input('post.sign');
            //验证token
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/register_rand";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if (!$res_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
                return $result_json;
            }
            //$model = new \app\index\model\User();
            if (isset($phone) && $phone != '') {
                $date = date('Y-m-d');
                $phone_rand_count = Cache::get($phone . $date . $type . 'count');
                if ($phone_rand_count >= 15) {
                    $result_json = ['code' => 199, 'msg' => '每天最多发送5条', 'content' => ''];
                    return $result_json;
                }
                $phone_pass = $this->phone_pass($phone);
                if (!$phone_pass) {
                    $result_json = ['code' => 199, 'msg' => '手机格式错误', 'content' => ''];
                    return $result_json;
                }
                //return 123;
                $send = new SmsMessage();
                $rand = $this->send_phoneRand();
                $arr = $send::sendSms($phone, $rand);
                //return json_encode($arr);
                $arr_array = json_decode(json_encode($arr), true);
                //return json_encode($arr_array);
                if ($arr_array['Code'] == 'OK') {
                    $phone_rand_count += 1;
                    Cache::set($phone . $date . $type . 'count', $phone_rand_count, strtotime('+1 day'));
                    Cache::set($phone . $type . "rand", $rand, 300);
                    //return Cache::get($phone . $type . "rand");
                    $result_json = ['code' => 200, 'msg' => '发送成功', 'content' => '发送成功'];
                    return $result_json;
                } else {
                    $result_json = ['code' => 199, 'msg' => '发送频率太快', 'content' => ''];
                    return $result_json;
                }
            } else {
                $result_json = ['code' => 199, 'msg' => '账号信息不正确', 'content' => ''];
                return $result_json;
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '请求方式错误', 'content' => ''];
            return $result_json;
        }
    }

    /**
     * @return mixed|string
     * 修改密码提交
     * @author  曹旺
     * @data  13:53
     */
    public function myselfinfo_save_pass()
    {
        $request = Request::instance();
        $data = $request->except(['sign', 'token', 'id']);
        $key = input('post.sign');
        //验证token
        $token = input('post.token');
        $type = input('post.type');
        //return json_encode($data);
        if ($type != "修改密码") {
            $json_result = ['code' => 199, 'msg' => "验证码类型错误", 'content' => '1'];
            return $json_result;
        }
        $token_pass = $this->encryption_token($token);
        if (!$token_pass) {
            $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
            return $json_result;
        }
        $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo_save_pass';
        $res_pass = $this->encryption($key, $value);
        if (!$res_pass) {
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
            return $result_json;
        }

        //实例化User  model
        $model = new \app\index\model\User();
        if (isset($data['email']) && $data['email'] != '') {
            //验证邮箱
            $validate = Loader::validate('Email');
            if (!$validate->check($data)) {
                $error = $validate->getError();
                $result_json = ['code' => 199, 'msg' => '邮箱格式错误', 'content' => $error];
                return $result_json;
            }
            //通过邮箱修改密码
            $result = $model->myselfinfo_save_pass_e($data);
            //return json_encode($result);
            if (!$result) {
                //修改密码失败
                $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                return $result_json;
            } else {
                //新旧密码一样时
                if ($result == 2) {
                    $result_json = ['code' => -1, 'msg' => '亲密码与旧密码不能一样', 'content' => ''];
                    return $result_json;
                } elseif ($result == 3) {
                    $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => '修改失败'];
                    return $result_json;
                } else {
                    //修改密码成功
                    $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '修改成功'];
                    return $result_json;
                }
            }
        } //验证手机及验证码
        elseif (isset($data['phone']) && $data['phone'] != '') {
            $phone = $this->phone_pass($data['phone']);
            //手机号格式错误
            if (!$phone) {
                $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                return $result_json;
            }
            //return json_encode($data);
            //通过手机修改密码
            $result = $model->myselfinfo_save_pass_p($data);
            //return json_encode($result);
            if ($result == 1) {
                //修改密码成功
                $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '修改成功'];
                return $result_json;
            } elseif ($result == 2) {
                //修改密码成功
                $result_json = ['code' => -1, 'msg' => '修改失败', 'content' => '新旧密码不能一样'];
                return $result_json;
            } elseif ($result == 3) {
                //修改密码成功
                $result_json = ['code' => 199, 'msg' => '验证码失效', 'content' => ''];
                return $result_json;
            } else {
                //修改密码成功
                $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                return $result_json;
            }

        }
    }

    /**
     * @return mixed|string
     * 绑定邮箱或手机
     * @author  曹旺
     * @data  15:06
     */
    public function myselfinfo_binding()
    {
        if ($this->request->isPost()) {
            $request = Request::instance();
            $data = $request->except(['sign', 'token']);
            $token = input('post.token');
            $type = input('post.type');
            if ($type != "绑定") {
                $json_result = ['code' => 199, 'msg' => "验证码类型错误", 'content' => '1'];
                return $json_result;
            }
            //return $token;
            $key = input('post.sign');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo_binding';
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => '1'];
                return $json_result;
            }
            $token_pass = $this->encryption_token($token);
            //return json_encode($token_pass);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => '2'];
                return $json_result;
            }
            $model = new \app\index\model\User();
            //提交邮箱验证
            if (isset($data['email']) && $data['email'] != '') {
                $validate = Loader::validate('Email');
                if (!$validate->check($data)) {
                    $error = $validate->getError();
                    $json_result = ['code' => 199, 'msg' => $error, 'content' => '3'];
                    return $json_result;
                }
                $result = $model->myselfinfo_binding_e($data);
                if ($result == 1) {
                    $json_result = ['code' => 200, 'msg' => "修改成功", 'content' => ''];
                    return $json_result;
                } elseif ($result == 2) {
                    $json_result = ['code' => -1, 'msg' => "新邮箱与旧邮箱一致", 'content' => ''];
                    return $json_result;
                } elseif ($result == 3) {
                    $json_result = ['code' => 199, 'msg' => "验证码失效", 'content' => ''];
                    return $json_result;
                } else {
                    $json_result = ['code' => 199, 'msg' => "修改失败", 'content' => ''];
                    return $json_result;
                }
            } //提交手机验证
            elseif (isset($data['phone']) && $data['phone'] != '') {
                $phone_pass = $this->phone_pass($data['phone']);
                if (!$phone_pass) {
                    $json_result = ['code' => 199, 'msg' => "手机号格式错误", 'content' => ''];
                    return $json_result;
                }
                $result = $model->myselfinfo_binding_p($data);
                if ($result == 1) {
                    $json_result = ['code' => 200, 'msg' => "修改成功", 'content' => ''];
                    return $json_result;
                } elseif ($result == 2) {
                    $json_result = ['code' => -1, 'msg' => "新手机号与旧手机号一致", 'content' => ''];
                    return $json_result;
                } elseif ($result == 3) {
                    $json_result = ['code' => 199, 'msg' => "验证码失效", 'content' => ''];
                    return $json_result;
                } else {
                    $json_result = ['code' => 199, 'msg' => "修改失败", 'content' => ''];
                    return $json_result;
                }

            } else {
                $json_result = ['code' => 404, 'msg' => "暂未开通的模块", 'content' => ''];
                return $json_result;
            }
        } else {
            $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => '4'];
            return $json_result;
        }
    }

    /**
     * @return mixed|string
     * 忘记密码
     * @author  曹旺
     * @data  13:08
     */
    public function myselfinfo_miss_pass()
    {
        if ($this->request->isPost()) {
            $key = input('post.sign');
            $request = Request::instance();
            $data = $request->except('sign');
            $type = input('post.type');
            if ($type != "忘记密码") {
                $json_result = ['code' => 199, 'msg' => "验证码类型错误", 'content' => '1'];
                return $json_result;
            }
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/User/myselfinfo_miss_pass';
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            //实例化User  model
            $model = new \app\index\model\User();
            if (isset($data['email']) && $data['email'] != '') {
                //验证邮箱
                $validate = Loader::validate('Email');
                if (!$validate->check($data)) {
                    $error = $validate->getError();
                    $result_json = ['code' => 199, 'msg' => '邮箱格式错误', 'content' => $error];
                    return $result_json;
                }
                //通过邮箱修改密码
                $result = $model->myselfinfo_save_pass_e($data);
                //return json_encode($result);
                if (!$result) {
                    //修改密码失败
                    $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                    return $result_json;
                } else {
                    //新旧密码一样时
                    if ($result == 2) {
                        $result_json = ['code' => -1, 'msg' => '新密码与旧密码不能一样', 'content' => ''];
                        return $result_json;
                    } elseif ($result == 3) {
                        $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => '修改失败'];
                        return $result_json;
                    } else {
                        //修改密码成功
                        $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '修改成功'];
                        return $result_json;
                    }
                }
            } //验证手机及验证码
            elseif (isset($data['phone']) && $data['phone'] != '') {
                $phone = $this->phone_pass($data['phone']);
                //手机号格式错误
                if (!$phone) {
                    $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                    return $result_json;
                }
                //return json_encode($data);
                //通过手机修改密码
                $result = $model->myselfinfo_save_pass_p($data);
                //return json_encode($result);
                if ($result == 1) {
                    //修改密码成功
                    $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '修改成功'];
                    return $result_json;
                } elseif ($result == 2) {
                    //修改密码成功
                    $result_json = ['code' => -1, 'msg' => '修改失败', 'content' => '新旧密码不能一样'];
                    return $result_json;
                } elseif ($result == 3) {
                    //修改密码成功
                    $result_json = ['code' => 199, 'msg' => '验证码失效', 'content' => ''];
                    return $result_json;
                } else {
                    //修改密码成功
                    $result_json = ['code' => 199, 'msg' => '修改失败', 'content' => ''];
                    return $result_json;
                }
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
            return $result_json;
        }
    }


    public function demo()
    {
        //echo  md5('13345678901');die;
        $send = new SmsMessage();
        $phone = 18354682311;
        $rand = $this->send_phoneRand();
        $arr = $send::sendSms($phone, $rand);
        dump($object = json_decode(json_encode($arr), true));
        die;
        $email = '592322241@qq.com';
        $rand = 12346546;
        $send_email = $this->email($email, $rand);
        echo $send_email;
    }

    public function demos()
    {
        //{"code":451066,"product":"dsd"}
        $arr = '{"code":123,"pas":"asdf"}';
        $arrr = json_decode($arr);
        dump($arrr);

    }

    public function demoss()
    {
        $array = ['user_id' => 1, 'commodity' => '[{"id":1,"number":2},{"id":1,"number":2}]'];
        echo $array['commodity'];
        $arr = json_decode($array['commodity'], true);
        dump($arr);
        echo $arr[0]['id'];

    }

    public
    function demosss()
    {
        $model = new SmsMessage();
        $model->
        // 调用示例：
        set_time_limit(0);
        header('Content-Type: text/plain; charset=utf-8');

        $response = SmsDemo::sendSms();
        echo "发送短信(sendSms)接口返回的结果:\n";
        print_r($response);

        sleep(2);

        $response = SmsDemo::sendBatchSms();
        echo "批量发送短信(sendBatchSms)接口返回的结果:\n";
        print_r($response);

        sleep(2);

        $response = SmsDemo::querySendDetails();
        echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
        print_r($response);
        $arr = [1, 2, 3, 4, 5, 6, 7, 8];
        $str = count($arr);
        for ($i = 0; $i < $str; $i++) {
            if ($arr[$i] == 3) {
                $aa = '123';
                break;
            } else {
                $aa = 321;
            }
        }
        echo $aa;
        die;
        $data = 'as';
        $datas = 'das';
        //Cache::set($data.$datas.'rand','asdffg',15);
        echo Cache::get($data . $datas . 'rand');
        die;

        //$phone=Cache::get('phonesqwqw');
        //dump($phone);die;
        if (!Cache::get('pho')) {
            Cache::set('rand', 1234567, 20);
            //$rand=Cache::get('rand');
            //$phone=1;
            echo 12;
            echo "<hr/>";
            Cache::set('pho', 1, 160);
        } else {
            $phone = Cache::get('pho');
            if ($phone < 5) {
                Cache::set('rand', 6543210, 20);
                $phone = $phone + 1;
            }
        }
        echo Cache::get('rand');
        echo "<hr/>";
        echo Cache::get('pho');
        die;
        //echo $aaa['phone']

    }
}